export function viewClick(view: any, callback: any) {
    // 监听图标点击事件
    view.on("click", (event: any) => {
        // 执行命中测试
        view.hitTest(event).then((response: any) => {
            if (response.results.length > 0) {
                const results = response.results;

                results.forEach((result: any) => {
                    const graphic = result.graphic;

                    // 处理并显示数据
                    if (typeof callback === "function") {
                        callback({
                            graphic: graphic,
                            mapPoint: event.mapPoint,
                        });
                    }
                });
            } else {
                console.log("未命中任何图形");
                if (typeof callback === "function") {
                    callback({
                        graphic: null,
                        mapPoint: event.mapPoint,
                    });
                }
            }
        });
    });
}

// 高亮要素
export function highlight(
    view2D: any,
    graphic: any,
    highlightOptions: object,
    layerViews: any
) {
    if (!graphic) {
        Object.entries(layerViews).map(([key]) => {
            layerViews[key]?.highlight?.remove();
        });
        return;
    }
    if (!layerViews[graphic.layer.id]) {
        layerViews[graphic.layer.id] = {};
        // 获取layerView并高亮要素
        view2D.whenLayerView(graphic.layer).then(function (layerView: any) {
            layerView.highlightOptions = highlightOptions;
            layerViews[graphic.layer.id]["layerView"] = layerView;
            layerViews[graphic.layer.id]["highlight"] = layerView?.highlight(graphic);
        });
    } else {
        Object.entries(layerViews).map(([key]) => {
            layerViews[key]?.highlight?.remove();
        });
        layerViews[graphic.layer.id]["highlight"] =
            layerViews[graphic.layer.id]["layerView"]?.highlight(graphic);
    }
}

// 清除高亮要素
export function clearHighlight(layerViews: any) {
    if (layerViews) {
        Object.entries(layerViews).map(([key]) => {
            layerViews[key]?.highlight?.remove();
        });
        return;
    }
}

